package controller;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import model.Simulator;

import tool.Database;

@SuppressWarnings("unused")
public class HunterController extends JFrame implements ActionListener
{
	// Object ID
	private static final long serialVersionUID = 6439589486813426532L;
	// Elements
	private Container contentPane;
	private JTextField textNaam;
	private JTextField textScore;
	private JButton saveButton;
	// Simulator 
	private Simulator simulator;
	
	/** Constructor */
	public HunterController(Simulator simulator)
	{
		this.simulator = simulator;
		createView();
	}
	
	/** Create view */
	public void createView()
	{
		// Frame settings
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setBounds(100, 100, 250, 100);
		
		// Create contentpane
		contentPane = getContentPane();
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		// Labels 
		
		JLabel labelNaam = new JLabel("Naam");
		labelNaam.setBounds(10,10,100,20);
		contentPane.add(labelNaam);
		
		JLabel labelScore = new JLabel("Beginscore");
		labelScore.setBounds(10,30,100,20);
		contentPane.add(labelScore);
		
		// Textfields
		
		textNaam = new JTextField();
		textNaam.setBounds(120,10, 100, 20);
		contentPane.add(textNaam);

		textScore = new JTextField();
		textScore.setBounds(120,30, 100, 20);
		contentPane.add(textScore);	
		
		// Buttons
		
		saveButton = new JButton("Save");
		saveButton.setBounds(120, 50, 75, 20);
		saveButton.addActionListener(this);
		contentPane.add(saveButton);
		
		// Appear
		setVisible(true);
	}

	/**
	 * Action handler (interface)
	 * @param ActionEvent arg0
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		if(arg0.getSource().equals(saveButton))
		{
			String hunterName = textNaam.getText();
			String hunterScore = textScore.getText();
			String query = "INSERT INTO VK.HUNTERS (NAME, SCORE) VALUES ('"+hunterName+"', "+hunterScore+")";

			try
			{
				Database.query(query);
				System.out.println("Hunter: " + hunterName + " with score: " + hunterScore + " inserted.");
			}
			catch(Exception e)
			{
				System.out.println(e.getMessage());
			}
		}
	}
	
	
}
